﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using LitJson;
using System.Web;

namespace He.API.WeiXin
{
    public class UserInfo
    {
        private int _subscribe;
        private string _openid;
        private string _nickname;
        private int _sex;
        private string _language;
        private string _city;
        private string _province;
        private string _country;
        private string _headimgurl;
        private List<string> _privilege;
        private int _subscribe_time;
        private string _unionid;

        public int subscribe
        {
            get
            {
                return this._subscribe;
            }
            set
            {
                this._subscribe = value;
            }
        }

        public string openid
        {
            get
            {
                return this._openid;
            }
            set
            {
                this._openid = value;
            }
        }

        public string nickname
        {
            get
            {
                return this._nickname;
            }
            set
            {
                this._nickname = value;
            }
        }
        public int sex
        {
            get
            {
                return this._sex;
            }
            set
            {
                this._sex = value;
            }
        }

        public string language
        {
            get
            {
                return this._language;
            }
            set
            {
                this._language = value;
            }
        }

        public string city
        {
            get
            {
                return this._city;
            }
            set
            {
                this._city = value;
            }
        }

        public string province
        {
            get
            {
                return this._province;
            }
            set
            {
                this._province = value;
            }
        }

        public string country
        {
            get
            {
                return this._country;
            }
            set
            {
                this._country = value;
            }
        }

        public string headimgurl
        {
            get
            {
                return this._headimgurl;
            }
            set
            {
                this._headimgurl = value;
            }
        }

        public List<string> privilege
        {
            get
            {
                return this._privilege;
            }
            set
            {
                this._privilege = value;
            }
        }

        public int subscribe_time
        {
            get
            {
                return this._subscribe_time;
            }
            set
            {
                this._subscribe_time = value;
            }
        }

        public string unionid
        {
            get
            {
                return this._unionid;
            }
            set
            {
                this._unionid = value;
            }
        }

        /// <summary>
        /// 根据OpenId获取用户信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(string openId)
        {
            string uri = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", CheckSignature.GetAccessToken(), openId);
            string result = HttpHelper.HttpGet(uri);
            UserInfo info = JsonMapper.ToObject<UserInfo>(result);
            return info;
        }

        /// <summary>
        /// 返回用户性别字符串
        /// </summary>
        /// <param name="sex"></param>
        /// <returns></returns>
        public static string GetSex(int sex)
        {
            switch (sex)
            {
                case 0:
                    return "未知";
                case 1:
                    return "男";
                case 2:
                    return "女";
                default:
                    return "未知";
            }
        }
    }
}
